mod common;
use crate::common::*;
use crate::user::ck_user;
use serde_json::json;

#[tokio::test]
async fn test_user_1() {
    do_ret(
        pos!(),
        json!({
            "line": "1",
            "v": {
                "sn": "",
                "passwd": "passwd",
                "name": "name_user1",
            }
        }),
        "user",
        "add",
        e_miss("sn"),
    )
    .await;

    do_ret(
        pos!(),
        json!({
            "line": "1",
        }),
        "user",
        "list",
        e_node(),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "v": {
                "sn": "user1",
                "passwd": "passwd",
                "name": "name_user1",
            }
        }),
        "user",
        "add",
    )
    .await;

    ck_user(pos!(), json!({
        "all": ["user1"],
        "user1": {"name": "name_user1", "sn": "user1", "passwd": "0D6BE69B264717F2DD33652E212B173104B4A647B7C11AE72E9885F11CD312FB"},
    })).await;

    ok_ret(
        pos!(),
        json!({
            "line": "1",
        }),
        "user",
        "list",
        json!([{"name": "name_user1", "sn": "user1"}]),
    )
    .await;

    ok_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "user1",
        }),
        "user",
        "get",
        json!({"name": "name_user1", "sn": "user1"}),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "user1",
        }),
        "user",
        "rm",
    )
    .await;

    do_ret(
        pos!(),
        json!({
            "line": "1",
        }),
        "user",
        "list",
        e_node(),
    )
    .await;

    do_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "user1",
        }),
        "user",
        "get",
        e_node(),
    )
    .await;

    ck_user(pos!(), json!({})).await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "user1",
        }),
        "user",
        "rm",
    )
    .await;

    ck_user(pos!(), json!({})).await;
}

#[tokio::test]
async fn test_user_2() {
    ok(
        pos!(),
        json!({
            "line": "1",
            "v": {
                "sn": "user1",
                "passwd": "passwd",
                "name": "name_user1",
            }
        }),
        "user",
        "add",
    )
    .await;

    ck_user(pos!(), json!({
        "all": ["user1"],
        "user1": {"name": "name_user1", "sn": "user1", "passwd": "0D6BE69B264717F2DD33652E212B173104B4A647B7C11AE72E9885F11CD312FB"},
    })).await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "v": {
                "sn": "user2",
                "passwd": "passwd",
                "name": "name_user2",
            }
        }),
        "user",
        "add",
    )
    .await;

    ck_user(pos!(), json!({
        "all": ["user1", "user2"],
        "user1": {"name": "name_user1", "sn": "user1", "passwd": "0D6BE69B264717F2DD33652E212B173104B4A647B7C11AE72E9885F11CD312FB"},
        "user2": {"name": "name_user2", "sn": "user2", "passwd": "0D6BE69B264717F2DD33652E212B173104B4A647B7C11AE72E9885F11CD312FB"},
    })).await;

    ok_ret(
        pos!(),
        json!({
            "line": "1",
        }),
        "user",
        "list",
        json!([
                {"sn": "user1", "name": "name_user1"},
                {"sn": "user2", "name": "name_user2"},
        ]),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "v": {
                "sn": "user1",
                "name": "n_user1",
            }
        }),
        "user",
        "md",
    )
    .await;

    ck_user(pos!(), json!({
        "all": ["user1", "user2"],
        "user1": {"name": "n_user1", "sn": "user1", "passwd": "0D6BE69B264717F2DD33652E212B173104B4A647B7C11AE72E9885F11CD312FB"},
        "user2": {"name": "name_user2", "sn": "user2", "passwd": "0D6BE69B264717F2DD33652E212B173104B4A647B7C11AE72E9885F11CD312FB"},
    })).await;

    ok_ret(
        pos!(),
        json!({
            "line": "1",
        }),
        "user",
        "list",
        json!([
                {"sn": "user1", "name": "n_user1"},
                {"sn": "user2", "name": "name_user2"},
        ]),
    )
    .await;

    ok_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "user1",
        }),
        "user",
        "get",
        json!({"sn": "user1", "name": "n_user1"}),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "user1",
        }),
        "user",
        "rm",
    )
    .await;

    ck_user(pos!(), json!({
        "all": ["user2"],
        "user2": {"name": "name_user2", "sn": "user2", "passwd": "0D6BE69B264717F2DD33652E212B173104B4A647B7C11AE72E9885F11CD312FB"},
    })).await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "user2",
        }),
        "user",
        "rm",
    )
    .await;

    ck_user(pos!(), json!({})).await;

    do_ret(
        pos!(),
        json!({
            "line": "1",
        }),
        "user",
        "list",
        e_node(),
    )
    .await;
}

#[tokio::test]
async fn test_user_3() {
    ok(
        pos!(),
        json!({
            "line": "1",
            "v": {
                "sn": "user1",
                "passwd": "passwd",
                "name": "name_user1",
            }
        }),
        "user",
        "add",
    )
    .await;

    ck_user(pos!(), json!({
        "all": ["user1"],
        "user1": {"name": "name_user1", "sn": "user1", "passwd": "0D6BE69B264717F2DD33652E212B173104B4A647B7C11AE72E9885F11CD312FB"},
    })).await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "v": {
                "sn": "user2",
                "passwd": "passwd",
                "name": "name_user2",
            }
        }),
        "user",
        "add",
    )
    .await;

    ck_user(pos!(), json!({
        "all": ["user1", "user2"],
        "user1": {"name": "name_user1", "sn": "user1", "passwd": "0D6BE69B264717F2DD33652E212B173104B4A647B7C11AE72E9885F11CD312FB"},
        "user2": {"name": "name_user2", "sn": "user2", "passwd": "0D6BE69B264717F2DD33652E212B173104B4A647B7C11AE72E9885F11CD312FB"},
    })).await;

    ok_ret(
        pos!(),
        json!({
            "line": "1",
            "sn": "user2",
            "passwd": "passwd",
        }),
        "user",
        "login",
        json!({"name": "name_user2", "sn": "user2"}),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "v": {
                "sn": "user1",
                "name": "n_user1",
                "passwd": "wd",
            }
        }),
        "user",
        "md",
    )
    .await;

    ck_user(pos!(), json!({
        "all": ["user1", "user2"],
        "user1": {"name": "n_user1", "sn": "user1", "passwd": "AEEFBB670B0875997E7748E0F26BE80A7D859410202123635792E413F8D0DFBE"},
        "user2": {"name": "name_user2", "sn": "user2", "passwd": "0D6BE69B264717F2DD33652E212B173104B4A647B7C11AE72E9885F11CD312FB"},
    })).await;

    do_ret(
        pos!(),
        json!({
            "line": "1",
            "sn": "user1",
            "passwd": "passwd",
        }),
        "user",
        "login",
        e_user("user passwd error"),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "user1",
        }),
        "user",
        "rm",
    )
    .await;

    ck_user(pos!(), json!({
        "all": ["user2"],
        "user2": {"name": "name_user2", "sn": "user2", "passwd": "0D6BE69B264717F2DD33652E212B173104B4A647B7C11AE72E9885F11CD312FB"},
    })).await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "user2",
        }),
        "user",
        "rm",
    )
    .await;

    do_ret(
        pos!(),
        json!({
            "line": "1",
        }),
        "user",
        "list",
        e_node(),
    )
    .await;

    ck_user(pos!(), json!({})).await;
}
